<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis as ISfRedis;
use DB;

//录像机

class VcrController extends Controller
{
    private $ysy_token_key = 'ysy_token';
    //列表展示
    public function index(Request $request) {
        if($request->expectsJson()) {
            //ajax请求
            //录像机 列表 datatable的ajax 加载
            $data = DB::table('a_vcr')->get();
            $jsonData['data'] = $data;
            return response()->json($jsonData);
        }
        else {
            return view('admin.vcr.index');
        }
    }

    //删除
    public function del(Request $request) {
        $id = $request->id;
        //  delete 返回受影响的行数
        $result = DB::table('a_vcr')->where('id', $id)->delete();
        //由于框架自身不支持响应bool值，所以转化一种形式
        return $result >= 0 ? '1' : '0';
    }

    //添加
    public function add(Request $request) {
        if($request->isMethod('POST')) {
            $post_data = $request->except(['_token','_form_token']);
            if(DB::table('a_vcr')->where('serial', $post_data['serial'])->exists()) {
                return '设备序列号已存在！';
            }
            $result = DB::table('a_vcr')->insert($post_data);
            //由于框架自身不支持响应bool值，所以转化一种形式
            return $result ? '1' : '0';
        }
        else {
            return view('admin.vcr.add');
        }
    }

    //修改
    public function edit(Request $request) {
        $id = $request->id;
        if($request->isMethod('POST')) {
            $post_data = $request->except(['_token','_form_token']);
            $where = [['id', '!=', $id], ['serial', '=', $post_data['serial']]];
            if(DB::table('a_vcr')->where($where)->exists()) {
                return '设备序列号已存在';
            }
            $result = DB::table('a_vcr')->where('id', $id)->update($post_data);
            //由于框架自身不支持响应bool值，所以转化一种形式
            return $result ? '1' : '请先修改数据！';
        }
        else {
            //查询要修改的录像机 信息
            $info = DB::table('a_vcr')->find($id);
            return view('admin.vcr.edit', compact('info'));
        }
    }

    //同步到萤石云
    public function ysyAdd(){
        $post_data = request()->only(['serial','valid_code']);
        //先删除再添加
        //删除账号下设备
        $url = 'https://open.ys7.com/api/lapp/device/delete';
        $postData = "accessToken={$this->getToken()}&deviceSerial={$post_data['serial']}";
        $res = json_decode(curl($url, $postData));
        if($res){
            if($res->code === '20031'){
                return $res->msg.'<br>登录萤石云视频客户端--“我的”--“通用设置”--“账号安全”--“终端绑定”';
            }
            if(in_array($res->code,['200','20002','20018'])) {
                //添加设备到账号下(萤石云)
                $url = 'https://open.ys7.com/api/lapp/device/add';
                $postData = "accessToken={$this->getToken()}&deviceSerial={$post_data['serial']}&validateCode={$post_data['valid_code']}";
                $res = json_decode(curl($url, $postData));
                if($res->code !== '200' || $res->code !== '20017') {
                    return $res->msg;
                }
                return 1;
            } 
            return $res->msg;
        }else{
            return '';
        }
        
        
    }

    //预览
    public function live(Request $request) {
        $id = $request->id;
        $info = DB::table('a_vcr')->find($id);
        $info->token = $this->getToken();
        return view('admin.vcr.live', compact('info'));
    }

    //获取萤石云token
    private function getToken() {
        $redis = ISfRedis::connection('public');
        if($token = $redis->get('ysy_token')) {
            return $token;
        }
        $url = 'https://open.ys7.com/api/lapp/token/get';
        $key = 'edfc38c549c546dbaaf7e4bf80afcc20';
        $secret = '2beb2a3fc8afb3aee262a2d22f103998';
        $postData = "appKey=$key&appSecret=$secret";
        $res = json_decode(curl($url, $postData));
        if($res){
            if($res->code == '200') {
                $seconds = 6*86400; //token缓存的时间，单位 秒
                $token = $res->data->accessToken;
                $redis->set('ysy_token', $token, 'EX', $seconds);
                return $token;
            }
            else {
                return '';
            }  
        }else{
            return '';
        }
        


    }
}
